﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace Com.Rivyer.Web.monitor
{
    public partial class tmppinglist : System.Web.UI.Page
    {
        private readonly BLL.tmppinglistBll pinglistbll = new BLL.tmppinglistBll();
        private readonly BLL.tmppingstatusBll pingstatusbll = new BLL.tmppingstatusBll();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ASPxButton2_Click(object sender, EventArgs e)
        {
            //清空表中数据,同时清空status里面的数据
            if (pinglistbll.GetAllList().Tables[0].Rows.Count == 0)
            {
                Maticsoft.Common.MessageBox.Show(this, "没有数据!");
            }
            else
            {
                pinglistbll.DeleteByUsername(Session["username"].ToString());
                pingstatusbll.DeleteByUsername(Session["username"].ToString());
                Maticsoft.Common.MessageBox.Show(this, "清除成功,刷新页面!");
                ASPxGridView1.DataBind();
            }
        }

        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
            //ping数据,然后写入到pinglist表以及pingstatus表中
            if (pinglistbll.GetAllList().Tables[0].Rows.Count == 0)
            {
                Maticsoft.Common.MessageBox.Show(this, "没有数据!");
            }
            else
            {
                pingstatusbll.DeleteByUsername(Session["username"].ToString());
                pingstatusbll.PingStatus(Session["username"].ToString(), ASPxSpinEdit1.Text == "" ? "0" : ASPxSpinEdit1.Text);
                //Maticsoft.Common.MessageBox.Show(this, "结束!");
            }
            /*
            List<object> selectedlist = ASPxGridView1.GetSelectedFieldValues("id");

            if (selectedlist.Count == 0)
            {
                Maticsoft.Common.MessageBox.Show(this, "没有任何数据被选中!");
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                foreach (int select in selectedlist)
                {
                    sb.Append(select + ",");
                }
                sb = sb.Remove(sb.ToString().LastIndexOf(','), 1);
                pingstatusbll.DeleteByUsername(Session["username"].ToString());
                pingstatusbll.PingStatus(Session["username"].ToString(), ASPxSpinEdit1.Text, sb);
                Maticsoft.Common.MessageBox.Show(this, "结束!");
            }
             */

        }

        protected void ASPxButton3_Click(object sender, EventArgs e)
        {
            //ping数据,然后写入到pinglist表中
            if (pinglistbll.GetAllList().Tables[0].Rows.Count == 0)
            {
                Maticsoft.Common.MessageBox.Show(this, "没有数据!");
            }
            else
            {
                pingstatusbll.DeleteByUsername(Session["username"].ToString());
                pinglistbll.PingFast(Session["username"].ToString());
                //Maticsoft.Common.MessageBox.Show(this, "结束!");
            }
        }

        protected void ASPxButton4_Click(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            ASPxGridViewExporter1.WriteXlsxToResponse(string.Format("{0}Ping数据", dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString()));
        }

        protected void ASPxGridView1_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            e.Cancel = true;
            if (Maticsoft.Common.PageValidate.IsIp(e.NewValues["ip"].ToString())
                && e.NewValues["description"] != null
                && e.NewValues["description"].ToString().Length != 0)
            {
                if (pinglistbll.GetModelList(string.Format("ip = '{0}'", e.NewValues["ip"].ToString())).Count != 0)
                {
                }
                //新增数据
                Model.tmppinglistModel listmodel = new Model.tmppinglistModel();
                listmodel.ip = e.NewValues["ip"].ToString();
                listmodel.username = Session["username"].ToString();
                listmodel.description = e.NewValues["description"].ToString();
                pinglistbll.Add(listmodel);
                ASPxGridView1.CancelEdit();
            }
        }

        protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {

        }
        
        protected void ASPxGridView1_RowInserting1(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            e.Cancel = true;
            if (e.NewValues["ip"] != null
                && Maticsoft.Common.PageValidate.IsIp(e.NewValues["ip"].ToString())
                && e.NewValues["description"] != null
                && e.NewValues["description"].ToString().Length != 0)
            {
                if (pinglistbll.GetModelList(string.Format("ip = '{0}' and username = '{1}'", e.NewValues["ip"].ToString(), Session["username"].ToString())).Count != 0)
                {
                }
                else
                {
                    //新增数据
                    Model.tmppinglistModel listmodel = new Model.tmppinglistModel();
                    listmodel.ip = e.NewValues["ip"].ToString();
                    listmodel.username = Session["username"].ToString();
                    listmodel.description = e.NewValues["description"].ToString();
                    pinglistbll.Add(listmodel);
                    ASPxGridView1.CancelEdit();
                }
            }
        }
    }
}